<?
global $db;
$e = $params;
$link = $e["link"];

#images
$e["content"] = preg_replace('/\<img(.*?)src\s*\=\s*\"([^"]+)\"(.*?)\>/i',
'<a href="edit.php?link=$2&parent='.$link.'"><img$1src="view.php?link=$2"$3></a>',$e["content"]);

#links
$e["content"] = preg_replace('/\<a(.*?)href\s*\=\s*\"([a-zA-Z0-9_\-\.]+)\"(.*?)\>/i',
'<a$1href="$2?parent='.$link.'"$3>',$e["content"]);

#code
function _wiki_code($m) {
	global $topdir;
	require_once "$topdir/ext/geshi/geshi.php";
	$content = $m[2];
	$content = preg_replace("/^\s*/s","",$content);
	$content = preg_replace("/\s*$/s","",$content);
	$geshi =& new GeSHi($content,$m[1]);
	return $geshi->parse_code(); 
	}
$e["content"] = preg_replace_callback('/\<code\s+class\s*\=\s*\"([^\"]+)\"\>(.*?)\<\/code>/is',_wiki_code,$e["content"]);

#grabbers {{link#section}}
function _wiki_grabber($m) {
	global $db;
	list($link,$section) = explode("#",$m[1]);
	$e = $db->query1("select * from wiki where ".
		" link = ".$db->sqlstring($link).
		" order by datetimeon desc ".
		" limit 1");
	$content = $e["content"];
	if (!$section) return $content;
	if ($v = _wiki_getsection($content,$section)) { return $v; }
	return "\{\{$link#$section}}";
	}
$e["content"] = preg_replace_callback('/\{\{([^\}]+)\}\}/',_wiki_grabber,$e["content"]);

#sections
global $_wiki_sections;
$_wiki_sections = array();
function _wiki_sections($m) {
	global $_wiki_sections;
	$_wiki_sections[] = array(
		"element"=>$m[1],
		"title"=>$m[2],
		);
	return "<a name=\"#{$m[2]}\"><{$m[1]}>{$m[2]}</{$m[1]}>";
	}
$e["content"] = preg_replace_callback('/\<(h\d)\>([^\>]+)\<\/\1>/i',_wiki_sections,$e["content"]);
	




#echo "<table cellpadding=0 cellspacing=0 border=0><tr><td class='content'>{$e["content"]}</table>";
#echo "<div contenteditable>{$e["content"]}</div>";
echo $e["content"];

#skin_param("mode","view");
#echo "<hr>";
#inc_incfile("wiki/comments.php",$e);
?>